package com.bluesmart.mia;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

public class GuideActivity extends BaseActivity implements OnClickListener{
	private List<View> lists = new ArrayList<View>();  
    private ViewPagerAdapter adapter;  
    
    int mCur = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		
		for(int i = 0; i < 6; i++)
		{
			View view = getLayoutInflater().inflate(R.layout.vp_guide, null);
			/*if(i > 0)*/ ((ImageView)view.findViewById(R.id.img)).setImageResource(R.drawable.g0 + i);
			lists.add(view);
			view.findViewById(R.id.left).setOnClickListener(this);
			view.findViewById(R.id.right).setOnClickListener(this);
			
			if(i > 0)
			{
				((TextView)view.findViewById(R.id.title)).setText(getString(R.string.guide_title_0 + i - 1));
				((TextView)view.findViewById(R.id.content)).setText(getString(R.string.guide_content_0 + i - 1));
			}
			else
			{
				((TextView)view.findViewById(R.id.content0)).setVisibility(View.VISIBLE);
				((TextView)view.findViewById(R.id.content0)).setText(getString(R.string.login_tip));
			}
		}
		
		lists.get(0).findViewById(R.id.left).setVisibility(View.INVISIBLE);
		lists.get(5).findViewById(R.id.right).setVisibility(View.INVISIBLE);

		adapter = new ViewPagerAdapter();  
		ViewPager viewPager = (ViewPager) findViewById(R.id.vp);  
        viewPager.setAdapter(adapter); 
        viewPager.setOnPageChangeListener(new OnPageChangeListener(){

			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}

			@Override
			public void onPageSelected(int arg0) {
				int tmp = mCur;
				mCur = arg0;
				
				if(mCur == 0) findViewById(R.id.dot_layout).setVisibility(View.GONE);
				else
				{
					findViewById(R.id.dot_layout).setVisibility(View.VISIBLE);
					if(tmp > 0) ((ImageView)findViewById(R.id.dot0 + tmp - 1)).setBackgroundResource(R.drawable.dot_normal);
					((ImageView)findViewById(R.id.dot0 + mCur - 1)).setBackgroundResource(R.drawable.dot_white);
				}
			}
        });
        
        findViewById(R.id.reg).setOnClickListener(this);
        findViewById(R.id.login).setOnClickListener(this);
	}
	
	protected void onResume()
	{
		super.onResume();
		
		String uri = "android.resource://" + getPackageName() + "/" + R.raw.login;
        VideoView videoView = (VideoView) findViewById(R.id.video);
        videoView.setVideoURI(Uri.parse(uri));
        videoView.start();
	}
	
	protected void onStop()
	{
		super.onStop();
		
		VideoView videoView = (VideoView) findViewById(R.id.video);
        videoView.stopPlayback();
	}
	
	public class ViewPagerAdapter extends PagerAdapter{
	    public ViewPagerAdapter()
	    {
	    }

	    @Override
	    public int getCount() {                                                                 //获得size
	        return lists.size();
	    }

	    @Override
	    public boolean isViewFromObject(View arg0, Object arg1) {                         
	        return arg0 == arg1;
	    }
	    
	    @Override
	    public void destroyItem(View view, int position, Object object)                       //销毁Item
	    {
	        ((ViewPager) view).removeView(lists.get(position));
	    }
	    
	    @Override
	    public Object instantiateItem(View view, int position)                                //实例化Item
	    {
	        ((ViewPager) view).addView(lists.get(position), 0);
	        return lists.get(position);
	    }
	}

	@Override
	public void onClick(View arg0) {
		if(arg0.getId() == R.id.reg)
		{
			startActivity(new Intent(this,PrivacyActivity.class));
			overridePendingTransition(R.anim.slide_in_from_top, R.anim.slide_out_to_bottom); 
		}
		else if(arg0.getId() == R.id.login)
		{
			startActivity(new Intent(this,LoginActivity.class));
			overridePendingTransition(R.anim.slide_in_from_top, R.anim.slide_out_to_bottom); 
		}
		else if(arg0.getId() == R.id.left)
		{
			ViewPager viewPager = (ViewPager) findViewById(R.id.vp);  
			viewPager.setCurrentItem(mCur - 1);
		}
		else if(arg0.getId() == R.id.right)
		{
			ViewPager viewPager = (ViewPager) findViewById(R.id.vp);  
			viewPager.setCurrentItem(mCur + 1);
		}
	}
}
